<?php
class ComponentController extends Zend_Controller_Action
{
	protected $_acl;
	protected $_userGroup;
	
	public function init()
	{
		$auth = Zend_Auth::getInstance();
		$userInfo = $auth->getStorage()->read();
		$groupBase = new GroupBase();
		if ($userInfo) {
			$userGroups = $groupBase->getUserGroup($userInfo->name);
		}
		$this->_userGroup = $userGroups[0]['name'] ? $userGroups[0]['name'] : "Users";
		$this->_acl = new Acl_PPMIS();
		$this->view->acl = $this->_acl;
		$this->view->userGroup = $this->_userGroup;
	}
	
	public function preDispatch()
	{
		$auth = Zend_Auth::getInstance();
		if($auth->getIdentity()) {
			$this->view->auth = "success";
			$userInfo = $auth->getStorage()->read();
			$this->view->userInfo = $userInfo;
		}
		$Configuration = new Configuration();
		$configurationData = $Configuration->getConfigurationInfo();
		$this->view->configuration = $configurationData;
	}
	public function indexAction()
	{
		$this->view->current = "project";
		$pid = $this->_getParam('pid');
		$project = new Project();
		$issue  = new Issue();
		$component = new Component();
		$components = $component->getData($pid);
		
		$projectData = $project->getAdapter()->fetchRow($project->select()->where('id',$pid));
		/*$projectSummary = $issue->getAdapter()->fetchAll($issue->select()
														->from('issue',array("issuestatus","count(*) As c"))
														->group('issuestatus'));
														
		/*$openIssues = $issue->getAdapter()->fetchAll($issue->select()
														->from('issue',array("priority","count(*) As c"))
														->group('priority'));*/
		$projectSummary = $issue->getIssueSummaryData();												
		$openIssues = $issue->getOpenIssuesData();
														
		/*$totalIssues = $issue->getAdapter()->fetchOne($issue->select()
																->from('issue',"count(*)"));*/
		$totalIssues = $issue->numRows();
																
		
		//Zend_Debug::dump($projectSummary);
		$this->view->projectData = $projectData;
		$this->view->totalIssues = $totalIssues;
		$this->view->projectSummary = $projectSummary;
		$this->view->openIssues = $openIssues;
		$this->view->components = $components;
		$this->view->projectId = $pid;
	}
	
	public function viewAction()
	{
		$this->view->current = "project";
		$projectId = $this->_getParam('project');
		$project = new Project();
		$component = new Component();
		$issue  = new Issue();
		$projectData = $project->getAdapter()->fetchRow($project->select()->where('id',$projectId));
		$data = $component->getAdapter()->fetchAll($component->select()->where('project = ?', $projectId));
		
		/*$projectSummary = $issue->getAdapter()->fetchAll($issue->select()
														->from('issue',array("issuestatus","count(*) As c"))
														->group('issuestatus'));
														
		$openIssues = $issue->getAdapter()->fetchAll($issue->select()
														->from('issue',array("priority","count(*) As c"))
														->group('priority'));
														
		$totalIssues = $issue->getAdapter()->fetchOne($issue->select()
																->from('issue',"count(*)"));*/
		$projectSummary = $issue->getIssueSummaryData();												
		$openIssues = $issue->getOpenIssuesData();
		$totalIssues = $issue->numRows();
														
		$this->view->projectData = $projectData;
		$this->view->totalIssues = $totalIssues;
		$this->view->projectSummary = $projectSummary;
		$this->view->openIssues = $openIssues;
		$this->view->components = $data;
		$this->view->projectId = $projectId;
		
	}
	
	public function addAction()
	{
		$form = new AddComponentForm();
		$component = new Component();
		
		if ($this->getRequest()->isPost()) {
			$formData = $this->_request->getPost();
			if ($form->isValid($formData)) {
				$data = array(
						'project' => $formData['project'],
						'cname' => $formData['cname'],
						'description' => $formData['description']
				);
				
				$component->insert($data);
			}
		}
		
		$this->view->form = $form;
	}
	
	public function editAction()
	{
		$cid = $this->_getParam('cid');
		$form = new EditComponentForm();
		$component = new Component();
		$componentData = $component->getAdapter()->fetchRow($component->select()->where('id=?',$cid));
		if ($this->getRequest()->isPost()) {
			$formData = $this->_request->getPost();
			if ($form->isValid($formData)) {
				$data = array(
						'project' => $formData['project'],
						'cname' => $formData['cname'],
						'description' => $formData['description']
				);
				
				$component->update($data, "id = ".$formData['id']);
				$this->_redirect('project/browse/project/'.$formData['project']);
			}
		}
		$componentData = $component->getAdapter()->fetchRow($component->select()->where('id=?',$cid));
		$form->populate($componentData);
		$this->view->form = $form;
	}
	
	public function deleteAction()
	{
		$cid = $this->_getParam('cid');
		$projectId = $this->_getParam('projectId');
		$this->_redirect('project/browse/project/'.$projectId);
	}
	
	public function openissuesAction()
	{
		$cid = $this->_getParam('cid');
		$projectId = $this->_getParam('projectId');
		$issue  = new Issue();
		$project = new Project();
		$projectVersion = new ProjectVersion();
		$component = new Component();
		
		$versionsData = $projectVersion->getComponentVersionData($projectId, $cid);
		$componentData = $component->getAdapter()->fetchRow($component->select()->where('id = ?',$cid));
		
		$componentData['pname'] = $project->getAdapter()->fetchOne($project->select()
														->from('project','pname')
														->where('id = ?',$projectId));
														
		$componentSummary = $issue->getComponentSummaryData($projectId,$cid);												
		$openIssues = $issue->getComponentOpenIssuesData($projectId, $cid);
		//$totalIssues = $issue->numRows();
		$totalIssues = 0;
		foreach ($componentSummary as $s) {
			$totalIssues = $totalIssues + $s['c'];
		}
		$componentIssues = $component->getComponentIssues($cid, $projectId);
		$unresolvedIssues = 0;
		foreach ($componentIssues as $cIssue) {
			if(!$cIssue['resolutionName']) {
				$unresolvedIssues += 1;		
			}
			$i++;
		}
		//echo $unresolvedIssues;
		//Zend_Debug::dump($componentIssues);
		$unscheduledIssues = $issue->getUnscheduledIssues($projectId);
		
		$this->view->unscheduleIssues = $unscheduledIssues;
		$this->view->componentIssues = $componentIssues;
		$this->view->componentData = $componentData;
		$this->view->totalIssues = $totalIssues;
		$this->view->componentSummary = $componentSummary;
		$this->view->openIssues = $openIssues;
		$this->view->versionsData = $versionsData;
		$this->view->projectId = $projectId;
		$this->view->unresolvedIssues = $unresolvedIssues;
	}
	
	public function roadmapAction()
	{
		$cid = $this->_getParam('cid');
		$projectId = $this->_getParam('projectId');
		$issue  = new Issue();
		$project = new Project();
		$component = new Component();
		$projectVersion = new ProjectVersion();
		
		$componentData = $component->getAdapter()->fetchRow($component->select()->where('id = ?',$cid));
		
		$componentData['pname'] = $project->getAdapter()->fetchOne($project->select()
														->from('project','pname')
														->where('id = ?',$projectId));
		
		$allVersion = $projectVersion->getProjectVersion($projectId);
		$i = 0;
		foreach ($allVersion as $version) {
			$allVersion[$i]['issues'] = $issue->getSingleComponentIssues($version['id'],$cid);
			$j = 0;
			$unresolvedIssues = 0;
			foreach ($allVersion[$i]['issues'] as $issues) {
			//Zend_Debug::dump($issues['resolutionName']);
				if (!$issues['resolutionName']) {
					$allVersion[$i]['issues'][$j]['resolutionName'] = '<font style="color:red">Unresolved</font>';
					$unresolvedIssues++;
				}
				$j++;
			}
			$allVersion[$i]['totalIssues'] = count($allVersion[$i]['issues']);
			$allVersion[$i]['unresolvedIssues'] = $unresolvedIssues;
			$allVersion[$i]['resolvedIssues'] = count($allVersion[$i]['issues']) - $unresolvedIssues;
			$i++;
		}
		//Zend_Debug::dump($allVersion);
		$componentSummary = $issue->getComponentSummaryData($projectId,$cid);												
		$openIssues = $issue->getComponentOpenIssuesData($projectId, $cid);
		//$totalIssues = $issue->numRows();
		$totalIssues = 0;
		foreach ($componentSummary as $s) {
			$totalIssues = $totalIssues + $s['c'];
		}
		
		$this->view->versionData = $allVersion;
		$this->view->totalIssues = $totalIssues;
		$this->view->componentSummary = $componentSummary;
		$this->view->openIssues = $openIssues;
		$this->view->componentData = $componentData;
		$this->view->projectId = $projectId;
		//$this->view->cid = $cid;
	}
	
}